#include <stdio.h>
#include <task.h>
	
const char S[]="SAVE_ME";
const char F[]="FIND";

	task_t t1, t2, serv; 
	
	


void *pong (void *arg){

	char message[] = "PONG";
	int message_size = sizeof (message);

	char *response;
	int response_size ;

		
	task_ns_register(&serv,&message);
	
	/*task_t client;
	
	task_receive (&response_size, sizeof(int));
	response = (char*) malloc (response_size);
	task_receive (response, response_size);
	printf("%s\n",response);
	free(response);
	
	task_send (&t2, &message_size, sizeof(int));
	task_send (&t2, message, message_size);*/
}


void *ping (void *arg){


	char message[] = "PING";
	int message_size = sizeof (message);

	char *response;
	int response_size ;

		
	task_ns_register(&serv,&message);
	/*char message[] = "pénoooooooooooooooo";
	int message_size = sizeof (message);
	char *response;
	int response_size ;
	task_cooperate();	
	
	task_send (&t1, &message_size, sizeof(int));
	task_send (&t1, message, message_size);
	task_receive (&response_size, sizeof(int));
	response = (char*) malloc (response_size);
	task_receive (response, response_size);
	printf("%s\n",response);
	free(response);*/
}



void *serveur (void *arg){



	task_t tache[15];
	char nom[15];
	int indice=0;
	int i;
	char *response;
	int response_size ;
	int message_size;
		
		task_receive (&response_size, sizeof(int));
		response = (char*) malloc (response_size);
		task_receive (response, response_size);//reception de l'action voulue
		printf("%s\n",response);
		if(strcmp(response,S)==0)
		{
			task_receive (response, response_size);
			response = (char*) malloc (response_size);
			task_receive (response, response_size);//ici on s'enregistre dans le serveur de nom
			//strcpy(nom[indice],response);
			printf("%s\n",response);
		}
		else if(strcmp(response,F)==0)
		{
			/*task_receive (response, response_size);
			for(i=0;i<indice;i++){
				if(mes_noms[i]==response){
					task_send (&t1, &message_size, sizeof(int));
					message_size=sizeof(adresse[i]);
					task_send (&t1, adresse[i], message_size);
					i=indice;
				}
			}*/
		}	
				
		//free(response);
		
	
}


void * main_task(void* arg){
	
	task_create(&serv, serveur, NULL); 
	task_create(&t1, pong, NULL);
	//task_create(&t2, ping, NULL);

	return NULL;
}

